﻿using System;
using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceProducts;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductPackageMappingService
    {
        public InsuranceProductPackageMapping LoadByID(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }

        public IQueryable<InsuranceProductPackageMapping> LoadByInsuranceProductPackageID(long insuranceProductPackageId)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductPackageMapping>().And(o => o.InsuranceProductPackageId == insuranceProductPackageId);
            return LoadEntities(expressionWhere);
        }

        public bool IsExist(long insuranceProductID, int insuranceProductPackageId, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductPackageMapping>().And(o => o.InsuranceProductPackageId == insuranceProductPackageId && o.InsuranceProductId == insuranceProductID);
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID == id);
            }
            return LoadEntities(expressionWhere).Any();
        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var entity = LoadByID(id);
            if (entity != null)
            {
                return DeleteEntities(entity);
            }
            return false;
        }

        public IQueryable<InsuranceProductPackageMapping> GetPagingList(InsuranceProductPackageMappingModel model,
            out int total)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductPackageMapping>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                expressionWhere = expressionWhere.And(o => (o.InsuranceProductPackage.ProductPackageName + SqlFunctions.Space(1) + o.InsuranceProducts.ProductName).Contains(model.Keyword));
            }
            if (model.InsuranceProductId > 0)
            {
                expressionWhere = expressionWhere.And(o => o.InsuranceProductId == model.InsuranceProductId);
            }
            if (model.InsuranceProductPackageId > 0)
            {
                expressionWhere = expressionWhere.And(o => o.InsuranceProductPackageId == model.InsuranceProductPackageId);
            }
            return LoadPagerEntities<InsuranceProductPackageMapping>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);

        }
        public bool DeleteBy(long iwrID, List<long> notDeleteIWRItemIds)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductPackageMapping>();
            expressionWhere = expressionWhere.And(o => o.InsuranceProductPackageId == iwrID);
            expressionWhere = expressionWhere.And(o => !notDeleteIWRItemIds.Contains(o.InsuranceProductId));
            var toBeDelList = LoadEntities(expressionWhere).ToList();
            var result = false;
            toBeDelList.ForEach(o =>
            {               
                result = DeleteEntities(o);
            });
            return result;
        }
    }
}